package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.utils.ExceptionResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.example.service.IRoleService;
import com.example.entity.Role;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-05-10
 */

@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleController {

        @Resource
        private IRoleService roleService;

        @PostMapping
        public ExceptionResult save(@RequestBody Role role) {
            return ExceptionResult.success(roleService.saveOrUpdate(role));
        }

        @DeleteMapping("/{id}")
        public ExceptionResult delete(@PathVariable Integer id) {
            return ExceptionResult.success(roleService.removeById(id));
        }

        @GetMapping
        public ExceptionResult findAll() {
            return ExceptionResult.success(roleService.list());
        }

        @GetMapping("/{id}")
        public ExceptionResult findOne(@PathVariable Integer id) {
            return ExceptionResult.success(roleService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public ExceptionResult deleteBatch(@RequestBody List<Integer> ids){
                return ExceptionResult.success(roleService.removeByIds(ids));
        }

        @GetMapping("/page")
        public ExceptionResult findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize
                                        ) {
                QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
                queryWrapper.orderByDesc("id");
                return ExceptionResult.success(roleService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }
}
